package org.deltaset.meta.internal;

import java.util.List;
import java.util.Map;

/**
 *
 * @author dmakariev
 */
public class MetaClassImpl<T> {

    
    private final String id;
    private final String scheme;
    private final String originalType;
    private final String collectionType;
    private final Map<String, MetaActionImpl> actions;
    private final Map<String, List<String>> actionsOrder;
    private final Map<String, MetaFieldImpl> fields;
    private final Map<String, List<String>> fieldsOrder;
    private final MetaActionImpl defaultAction;
    private final MetaActionImpl findByIdAction;
    private final MetaFieldImpl idField;
    private final boolean isCollection;
    private final boolean isVoid;
    private final boolean isPojo;
    private final boolean isEmpty;
    private final T detail;

    MetaClassImpl(
            String id,
            String scheme,
            String originalType,
            String collectionType,
            Map<String, MetaActionImpl> actions,
            Map<String, List<String>> actionsOrder,
            Map<String, MetaFieldImpl> fields,
            Map<String, List<String>> fieldsOrder,
            MetaActionImpl defaultAction,
            MetaActionImpl findByIdAction,
            MetaFieldImpl idField,
            boolean isCollection,
            boolean isVoid,
            boolean isPojo,
            boolean isEmpty,
            T detail) {
        this.id = id;
        this.scheme = scheme;
        this.originalType = originalType;
        this.collectionType = collectionType;
        this.actions = actions;
        this.actionsOrder = actionsOrder;
        this.fields = fields;
        this.fieldsOrder = fieldsOrder;
        this.defaultAction = defaultAction;
        this.findByIdAction = findByIdAction;
        this.idField = idField;
        this.isCollection = isCollection;
        this.isVoid = isVoid;
        this.isPojo = isPojo;
        this.isEmpty = isEmpty;
        this.detail = detail;
    }

    public List<MetaActionImpl> getActions() {
        return MetaUtils.orderedFor(actions, actionsOrder, MetaUtils.DEFAULT);
    }

    public String getCollectionType() {
        return collectionType;
    }

    public MetaActionImpl getDefaultAction() {
        return defaultAction;
    }

    public MetaActionImpl getFindByIdAction() {
        return findByIdAction;
    }

    public List<MetaFieldImpl> getFields() {
        return MetaUtils.orderedFor(fields, fieldsOrder, MetaUtils.DEFAULT);
    }

    public List<MetaFieldImpl> getFieldsFor(String viewName) {
        return MetaUtils.orderedFor(fields, fieldsOrder, viewName);
    }

    public List<MetaActionImpl> getActionsFor(String viewName) {
        return MetaUtils.orderedFor(actions, actionsOrder, viewName);
    }

    public String getId() {
        return id;
    }

    public MetaFieldImpl getIdField() {
        return idField;
    }

    public boolean isCollection() {
        return isCollection;
    }

    public boolean isVoid() {
        return isVoid;
    }

    public boolean isPojo() {
        return isPojo;
    }
    
    public boolean isPrimitive() {
        return !isPojo;
    }
    
    public boolean isEmpty() {
        //return fields.isEmpty() && actions.isEmpty();
        return isEmpty;
    }

    public String getOriginalType() {
        return originalType;
    }

    public String getScheme() {
        return scheme;
    }

    public T getDetail() {
        return detail;
    }

    public Class getDetailClass() {
        return detail.getClass();
    }

    public boolean hasId() {
        return !this.getIdField().isEmpty();
    }

    

    
}
